package com.generator.view.util;

import java.io.*;
import java.util.Properties;

/**
 * ${description}
 *
 * @author jaybril
 * @date 2019-06-02 16:54
 **/
public class CookieUtils {

    private final static String fileName= "t/user.properties";

    private static FileOutputStream OUT;
    private static InputStream IN;
    static {

        try {
            if(!new File(fileName).getParentFile().exists()){
                new File(fileName).getParentFile().mkdirs();
            }
            OUT = new FileOutputStream(fileName, false);
            if (new File(fileName).exists()) {
                IN = new BufferedInputStream(new FileInputStream(fileName));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void setCookie(String key, String value) {
        Properties prop = new Properties();
        try {
            prop.setProperty(key, value);
            prop.store(OUT, null);
            OUT.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getCookie(String key) {
        if(IN==null){
            return null;
        }
        Properties prop = new Properties();
        try {
            prop.load(IN);
            return prop.getProperty(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Boolean removeCookie(Object key) {
        if(IN==null){
            return null;
        }
        Properties prop = new Properties();
        try {
            prop.load(IN);
            prop.remove(key);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
